package com.shop.manager.controller;

//import com.shop.manager.entity.Address;
//import com.base.common.entity.Address;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.Address;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.AddressService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 地址信息增删改查
 */
@RestController
@RequestMapping("/address")
@Api(value = "地址信息增删改查表现层", protocols = "http", description = "地址信息增删改查表现层")
public class AddressController {

    /**
     * @Description 注入service对象
     */
    @Resource(type = AddressService.class)
    private AddressService addressService;

    /**
     * @Description 新增地址信息
     * @param address 地址对象
     * @return
     */
    @ApiOperation(value = "新增地址", notes = "接收地址，新增地址信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "新增地址成功"),
            @ApiResponse(code = 500, message = "新增地址失败,后台服务出现异常"),
            @ApiResponse(code = 401, message = "代表此操作无权限访问"),
            @ApiResponse(code = 400, message = "表示请求参数错误"),
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) @ApiParam(name = "地址信息", value = "传入json格式") Address address) {
        try {
            addressService.add(address);
            return HttpResult.ok("添加地址成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加地址失败!");
    }

    /**
     * @Description 批量删除地址信息
     * @param ids 地址id的数组
     * @return
     */
    @ApiOperation(value = "根据id删除地址", notes = "接收数组ids")
    @ApiResponses({
            @ApiResponse(code = 200, message = "批量删除地址成功"),
            @ApiResponse(code = 500, message = "批量删除地址失败,后台服务出现异常"),
            @ApiResponse(code = 401, message = "代表此操作无权限访问"),
            @ApiResponse(code = 400, message = "表示请求参数错误"),
    })
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public HttpResult delete(@RequestBody(required = false) @ApiParam(name = "要删除的地址id数组", type = "Long[]") Long[] ids) {
        try {
            addressService.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改地址信息
     * @param address 修改的地址对象
     * @return
     */
    @ApiOperation(value = "修改地址", notes = "接收地址信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改地址成功"),
            @ApiResponse(code = 500, message = "修改地址失败,后台服务出现异常"),
            @ApiResponse(code = 401, message = "代表此操作无权限访问"),
            @ApiResponse(code = 400, message = "表示请求参数错误"),
    })
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public HttpResult update(@RequestBody(required = false) @ApiParam(name = "地址信息", value = "传入json格式", required = false) Address address) {
        try {
            addressService.update(address);
            return HttpResult.ok("修改地址成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改地址失败!");
    }

    /**
     * @Description 根据id查询地址信息
     * @param id 地址id
     * @return
     */


    @ApiOperation(value = "根据id查询地址", notes = "接收地址id")
    @ApiImplicitParam(paramType = "path", name = "id", value = "地址id", required = true, dataType = "Long")
    @ApiResponses({
            @ApiResponse(code = 200, message = "根据id查询地址成功"),
            @ApiResponse(code = 500, message = "根据id查询地址失败,后台服务出现异常"),
            @ApiResponse(code = 401, message = "代表此操作无权限访问"),
            @ApiResponse(code = 400, message = "表示请求参数错误"),
    })
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public HttpResult getById(@RequestParam("id") Long id) {
        return HttpResult.ok(addressService.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param address 查询条件对象
     * @return
     */
    @ApiOperation(value = "地址分页带条件查询", notes = "接受分页参数pageNum,size，address查询条件对象")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "第几页", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "size", value = "一页显示多少条数据", required = false, dataType = "int")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "条件分页查询地址成功"),
            @ApiResponse(code = 500, message = "条件分页查询地址失败,后台服务出现异常"),
            @ApiResponse(code = 401, message = "代表此操作无权限访问"),
            @ApiResponse(code = 400, message = "表示请求参数错误"),
    })
    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    public HttpResult getAll(@RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum, @RequestParam(value = "size", defaultValue = "10") Integer size, @RequestBody(required = false) @ApiParam(value = "搜索地址信息条件") Address address) {
        try {
            //分页查询
            PageResult result = addressService.findPage(pageNum, size, address);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询地址异常");
        }
    }

    /**
     * @Description 分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @return
     */
    @ApiOperation(value = "地址分页查询", notes = "接受分页参数pageNum,size")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "第几页", required = false, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "size", value = "一页显示多少条数据", required = false,  dataType = "int")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "条件分页查询地址成功"),
            @ApiResponse(code = 500, message = "条件分页查询地址失败,后台服务出现异常"),
            @ApiResponse(code = 401, message = "代表此操作无权限访问"),
            @ApiResponse(code = 400, message = "表示请求参数错误"),
    })
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public PageResult findAllAttr(@RequestParam(value = "pageNum", defaultValue = "1")  Integer pageNum, @RequestParam(value = "size", defaultValue = "10") Integer size) {
        PageResult pageResult = addressService.findPage(pageNum, size);
        return pageResult;
    }
}
